/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    internal partial class ActionEditor : EditorWindow
    {
        public static ActionEditor Instance;
        private VisualElement _root;

        [MenuItem("TFrameWork/Module/ActionEditor")]
        public static void ShowWindow()
        {
            ActionEditor wnd = GetWindow<ActionEditor>();
            wnd.titleContent = new GUIContent("ActionEditor");
            wnd.minSize = new Vector2(800, 600);
            GUIContent content = EditorGUIUtility.IconContent("snapIncrement");
            content.text = "Action Editor";
            wnd.titleContent = content;
            wnd.Show();
        }

        public void CreateGUI()
        {
            if (Instance == null)
            {
                Instance = this;
            }

            //注册数据发送改变
            ActionData.SetValidateAction(OnActionDataChanged);

            _root = rootVisualElement;
            VisualTreeAsset visualTree = null;
            foreach (var guid in AssetDatabase.FindAssets("t:VisualTreeAsset"))
            {
                string path = AssetDatabase.GUIDToAssetPath(guid);
                if (path.Contains("ActionEditor.uxml"))
                {
                    visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(path);
                    break;
                }
            }

            if (visualTree != null)
            {
                visualTree.CloneTree(_root);
                
                InitConfig();
                InitTopMenu();
                InitTimerShaft();
                InitConsole();
                InitContent();
                InitData();
                
                Undo.undoRedoPerformed -= OnUndoRedo;
                Undo.undoRedoPerformed += OnUndoRedo;
            }
        }

        public void ClearUndo()
        {
            Undo.undoRedoPerformed -= OnUndoRedo;
        }

        public void AddUndo()
        {
            Undo.undoRedoPerformed += OnUndoRedo;
        }
        
        private void OnUndoRedo()
        {
            isRedo = true;
            RefreshWindow();
            AssetDatabase.SaveAssets(); 
            isRedo = false;
        }

        public bool isRedo = false;

        /// <summary>
        /// 当配置数据发生改变 
        /// </summary>
        private void OnActionDataChanged()
        {


            RefreshWindow();

        }

        public void RefreshWindow()
        {
            //暴力刷新
            ActionData tempActionData = _actionData;
            ActionDataObjectField.value = null;
            ActionDataObjectField.value = tempActionData;
        }

        private void OnGUI()
        {
            OnGUITop();
            GetObjectPickerObjectGUI();
        }
        
        private void OnEnable()
        {
            SceneView.duringSceneGui -= OnSceneGUI;
            SceneView.duringSceneGui += OnSceneGUI;
        }

        private void OnDisable()
        {
            SceneView.duringSceneGui -= OnSceneGUI;
            if (_actionData != null) SaveConfig();
        }
        
        // 窗口销毁会调用OnDestroy，但是直接关闭Unity不会调用
        private void OnDestroy()
        {
            Instance = null;
        }
    }
}
